﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DBSqlSugar
{
    public class SplitService : ISplitTableService
    {
        /// <summary>
        /// 获取对应的分表服务
        /// </summary>
        /// <param name="entityInfo"></param>
        /// <returns></returns>

        private ISplitTableService GetSplitTableService(EntityInfo entityInfo)
        {
            var splitType = entityInfo.Type.GetCustomAttribute<SplitTableAttribute>().SplitType;

            switch (splitType)
            {
                case SplitType.Year: return new DateSplitTableService(); //按年分表
                case SplitType.Month: return new DateSplitTableService();  //按月分表
                case SplitType.Day: return new DateSplitTableService();  //按日分表
                case SplitType.Season: return new DateSplitTableService();//按季度分表
                case SplitType._Custom01: return new KeySplitTableService();//自定义1：按关键字分表
            }

            throw new Exception("未定义对应的分表服务！");
        }

        /// <summary>
        /// 返回数据库中所有分表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityInfo"></param>
        /// <param name="tableInfos"></param>
        /// <returns></returns>

        public List<SplitTableInfo> GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo, List<DbTableInfo> tableInfos)
        {
            var service = GetSplitTableService(EntityInfo);
            return service.GetAllTables(db, EntityInfo, tableInfos);
        }



        /// <summary>
        /// 获取分表字段的值
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entityInfo"></param>
        /// <param name="splitType"></param>
        /// <param name="entityValue"></param>
        /// <returns></returns>
        public object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue)
        {
            var service = GetSplitTableService(entityInfo);
            return service.GetFieldValue(db, entityInfo, splitType, entityValue);
        }

        /// <summary>
        /// 默认表名
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entityInfo"></param>
        /// <returns></returns>
        public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo)
        {
            var service = GetSplitTableService(entityInfo);
            return service.GetTableName(db, entityInfo);
        }

        public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType type)
        {
            var service = GetSplitTableService(entityInfo);
            return service.GetTableName(db, entityInfo, type);
        }

        public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue)
        {
            var service = GetSplitTableService(entityInfo);
            return service.GetTableName(db, entityInfo, splitType, fieldValue);
        }

    }


}
